//
//  DataCenter.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-25.
//
//

#include "DataCenter.h"
#include "string.h"
#include "DictionaryKeyNames.h"

#define splite                      '|'

DataCenter * DataCenter ::_dataCenter = nullptr;

DataCenter* DataCenter::getInstance()
{
    if (! _dataCenter)
    {
        _dataCenter = new DataCenter();
    }
    return _dataCenter;
}


std::string DataCenter::getCupNameByType(int type)
{
    if (type == 0) {
        return "btn_addcup@2x.png";
    }
    CCASSERT(type > 0 && type <= getCupCount(), "index out of bounds");
    return StringUtils::format("cup_%02d_d@2x.png", type);
}

std::string DataCenter::getDCupNameByType(int type)
{
    CCASSERT(type > 0 && type < 11, "index out of bounds");
    return StringUtils::format("d_cup_%02d_d@2x.png", type);
}

int DataCenter::getCupCount()
{
    return 10;
}


void DataCenter::setValueVectorForKey(ValueVector &vector, const char *key)
{
    std::string valueString = "";
    for (int i = 0; i < vector.size(); i++) {
        valueString = valueString + vector.at(i).asString();
        if (i != vector.size() - 1) {
            valueString += splite;
        }
    }
    UserDefault::getInstance() -> setStringForKey(key, valueString);
}

ValueVector DataCenter::getValueVectorByKey(const char *key)
{
    ValueVector valueVector;
    std::string valueString = UserDefault::getInstance() -> getStringForKey(key);
    std::string::iterator i;
    std::string tmp = "";
    for(i = valueString.begin(); i <= valueString.end(); ++i) {
        if((const char)*i != splite  && i != valueString.end()) {
            tmp += *i;
        } else {
            valueVector.push_back(Value(tmp));
            tmp = "";
        }
    }
    return valueVector;
}


ValueVector DataCenter::getValueVectorByKey(const char *key, ValueVector vector)
{
    ValueVector valueVector = this -> getValueVectorByKey(key);
    return valueVector.size() == 0 ? vector : valueVector;
}

int DataCenter::getSceneCount()
{
    return 7;
}

std::string DataCenter::getMoreSceneByType(int type)
{
    return StringUtils::format("more_scene%d.png", type);
}
std::string DataCenter::getBackgroundSceneByType(int type)
{
    return StringUtils::format("backgroundscene%d.png", type);
}

int DataCenter::getCapacityHaveDrink()
{
    return UserDefault::getInstance() -> getIntegerForKey(kHaveDrink, 0);
}

void DataCenter::drink(int cap)
{
    if (cap <= 0) {
        cap = DataCenter::getUsingCupCapacity();
    }
    _haveDrink += cap;
    UserDefault::getInstance() -> setIntegerForKey(kHaveDrink, _haveDrink);
}

int DataCenter::getCapacityToDrink()
{
    return 2000;
}

std::string DataCenter::getUsingCupName()
{
    return DataCenter::getInstance() -> getDCupNameByType(getUsingCupType());
}

int DataCenter::getUsingCupType()
{
    return UserDefault::getInstance() -> getIntegerForKey(kUsingDCup, 1);
}

int DataCenter::getUsingCupCapacity()
{
    return UserDefault::getInstance() -> getIntegerForKey(kUsingCupCapacity, 100);
}

std::string DataCenter::getUsingBackgroundSceneName()
{
    int type = UserDefault::getInstance() -> getIntegerForKey(kBackgroundScene, 1);
    return DataCenter::getInstance() -> getBackgroundSceneByType(type);
}

void DataCenter::setUsingCupType(int type)
{
    UserDefault::getInstance() -> setIntegerForKey(kUsingDCup, type);
}

void DataCenter::setUsingCupCapacity(int cap)
{
    UserDefault::getInstance() -> setIntegerForKey(kUsingCupCapacity, cap);
}

void DataCenter::setUsingBackgroundSceneType(int type)
{
    UserDefault::getInstance() -> setIntegerForKey(kBackgroundScene, type);
}

void DataCenter::clear()
{
    this -> setUsingCupType(1);
    this -> setUsingBackgroundSceneType(1);
    this -> setUsingCupCapacity(100);
    UserDefault::getInstance() -> setIntegerForKey(kHaveDrink, 0);
    log("数据删除~");
}